package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.apkextract.lib.model.AeUtil;
import com.stark.file.transfer.core.FileInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableSendManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AppAdapter;
import flc.ast.adapter.SelAudioAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.databinding.ActivityAppTransferBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.constant.FileType;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;

public class AppTransferActivity extends BaseAc<ActivityAppTransferBinding> {

    private AppAdapter appAdapter;
    private List<ApkBean> listSel = new ArrayList<>();
    private boolean isSelectAll = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_app_transfer;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivAppTransferBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvAppList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        appAdapter = new AppAdapter();
        appAdapter.setEdit(true);
        mDataBinding.rvAppList.setAdapter(appAdapter);
        appAdapter.setOnItemClickListener(this);
        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.tvStartTransfer.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSelectAll:
                listSel.clear();
                if (isSelectAll) {
                    for (ApkBean validDatum : appAdapter.getValidData()) {
                        validDatum.setSelected(false);
                    }
                } else {
                    for (ApkBean validDatum : appAdapter.getValidData()) {
                        validDatum.setSelected(true);
                        listSel.add(validDatum);
                    }
                }
                isSelectAll();
                appAdapter.notifyDataSetChanged();
                break;
            case R.id.tvStartTransfer:
                if (listSel.size() == 0) {
                    ToastUtils.showShort(R.string.no_choose_file_transfer_tips);
                    return;
                }
                List<Transferable> transferableList = new ArrayList<>();
                for (ApkBean bean : listSel) {
                    FileInfo appFile = new FileInfo();
                    appFile.setFileName(AppUtils.getAppName(bean.pkgName));
                    appFile.setUriStr((bean.pkgName));
                    appFile.setFileType(FileType.APK);
                    AppUtils.AppInfo appInfo;
                    appInfo = AppUtils.getAppInfo(bean.pkgName);
                    File apkFile = new File(appInfo.getPackagePath());
                    appFile.setSize(apkFile.length());
                    transferableList.add(appFile);
                }
                TransferableSendManager.getInstance().setTransferables(transferableList);
                startActivity(FileSendShowQrActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (appAdapter.getItem(position).isSelected()) {
            appAdapter.getItem(position).setSelected(false);
            listSel.remove(appAdapter.getItem(position));
            appAdapter.notifyDataSetChanged();
        } else {
            listSel.add(appAdapter.getItem(position));
            appAdapter.getItem(position).setSelected(true);
            appAdapter.notifyDataSetChanged();
        }
        isSelectAll();
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        loadApks();
    }

    private void loadApks() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<String>>() {
            @Override
            public void doBackground(ObservableEmitter<List<String>> emitter) {
                emitter.onNext(AeUtil.getNoSystemInstalledPackageNames());
            }

            @Override
            public void accept(List<String> packageNames) {
                if (!CollectionUtils.isEmpty(packageNames)) {
                    mDataBinding.rvAppList.setVisibility(View.VISIBLE);
                    mDataBinding.ivAppTransferNoData.setVisibility(View.GONE);
                    List<ApkBean> apkBeans = new ArrayList<>();
                    ApkBean bean;
                    for (String packageName : packageNames) {
                        bean = new ApkBean();
                        bean.pkgName = packageName;
                        apkBeans.add(bean);
                    }
                    appAdapter.setList(apkBeans);
                } else {
                    mDataBinding.rvAppList.setVisibility(View.GONE);
                    mDataBinding.ivAppTransferNoData.setVisibility(View.VISIBLE);
                }
                dismissDialog(500L);
            }
        });
    }

    private void isSelectAll() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listSel.size()));
        if (listSel.size() == appAdapter.getValidData().size()) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.ayiquanx);
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanx);
            isSelectAll = false;
        }
    }

}